/*  Novelties: A visual novel programming language based on JavaFX
    Copyright (C) 2008  Hao Xu
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * /
 /*
 * Sprite.fx
 *
 * Created on Apr 7, 2008, 12:07:32 AM
 */

package avgengine;

import javafx.ui.canvas.*;
import javafx.ui.*;
import java.lang.Thread;
/**
 * @author ertri
 */

class Sprite extends Picture {
    attribute imageSet:Image*;
    attribute halignment:int;
    attribute valignment:int;
    attribute offset:int;
    attribute stage: Dialog;
    attribute scalar: int*;
    operation enter(side:int, x:int);
    operation quickAdjust(x:int);
    operation translateCoordinate(x:int):int*;
    operation translateCoordinateOff(x:int, off:int):int*;
    operation exit(side:int);
    operation quickExit(side:int);
    operation move(x:int);
    operation moveOff(x:int, off:int);
    operation quickMove(x:int*);
    operation show(i:int);
    operation quickShow(i:int);
    operation quickClear();
}

LEFT:int = 0;
RIGHT:int = 1;
TOP:int = 2;
BOTTOM:int = 3;

CENTER:int = 4;

attribute Sprite.noBG = true;
attribute Sprite.pad = [0,0];
attribute Sprite.scalar = [1,1];

operation Sprite.enter(side, finishPos) {
    
    var coor = this.translateCoordinate(finishPos);
    var tx = coor[0];
    var ty = coor[1];
    x = if side == LEFT:int then -coor[2] else
        (if side == RIGHT:int then stage.dwidth else
            tx);
        y = if side == TOP:int then -coor[3] else
            (if side == BOTTOM:int then stage.dheight else
                ty);
            var anim = Animation{};
            anim.justTranslate(this, [tx, ty], speed);
            
            
}

operation Sprite.translateCoordinate(finishPos) {
    var h;
    var img = image.getImage();
    h = img.getHeight(null);
    while(h==-1){
        Thread.sleep(100);
        h = img.getHeight(null);
    }
    var w = img.getWidth(null);
    if(h ==null) {
        h = 0; }
    if(w==null) {
        w = 0; }
    
    println("{image.getImage()},{h},{w}");
    println("{stage},{stage.dheight},{offset},{scalar[1]}");
    var ty;
    if(valignment == TOP:int) {
        ty = offset*scalar[1];
    } else if (valignment == CENTER:int) {
        ty = offset*scalar[1] + stage.dheight/2;
    } else  {
        ty = stage.dheight - offset*scalar[1] - h;
    }
    
    var tx = finishPos*scalar[0] + -(if this.halignment == LEFT:int then 0
    else (if this.halignment == RIGHT:int then w else w/2));
    println("{tx},{ty},{w},{h}");
    return [tx, ty, w, h];
    
}

operation Sprite.exit(side) {
    var h = image.getImage().getHeight(null);
    var w = image.getImage().getWidth(null);
    var tx = (if side == LEFT:int then -w else (if side == RIGHT:int then stage.dwidth else x));
    var ty = (if side == TOP:int then -h else (if side == BOTTOM:int then stage.dheight else y));
    var anim = Animation{};
    anim.justTranslate(this, [tx, ty], speed);
}

operation Sprite.quickAdjust(tx) {
    var coor = this.translateCoordinate(tx);
    y = coor[0];
    x = coor[1];
}

operation Sprite.quickExit(side) {
    var h = image.getImage().getHeight(null);
    var w = image.getImage().getWidth(null);
    var tx = (if side == LEFT:int then -w else stage.dwidth);
    this.x = tx;
}

operation Sprite.move(pos) {
    var coor = this.translateCoordinate(pos);
    var anim = Animation{};
    anim.justTranslate(this, [coor[0],coor[1]], speed);
}

operation Sprite.moveOff(pos,off) {
    offset = off;
    var coor = this.translateCoordinate(pos);
    var anim = Animation{};
    anim.justTranslate(this, [coor[0],coor[1]], speed);
}

operation Sprite.quickMove(pos) {
    if(sizeof pos == 2) {
        offset = pos[1];
        var coor = this.translateCoordinate(pos[0]);
        println("{coor[0]},{coor[1]}");
        this.x = coor[0];
        this.y = coor[1];
    } else {
        var coor = this.translateCoordinate(pos[0]);
        
        this.x = coor[0];
        this.y = coor[1];
    }
}

operation Sprite.show(i) {
    
    this.replacePicture(imageSet[i]);
}

operation Sprite.quickShow(i) {
    var speed = this.speed;
    this.speed = 10;
    show(i);
    this.speed = speed;
}
operation Sprite.quickClear() {
    var speed = this.speed;
    this.speed = 10;
    this.clearText();
    this.speed = speed;
}

